Security News
Research
Supply Chain Attack on Rspack npm Packages Injects Cryptojacking Malware
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
troika-worker-utils
Advanced tools
The troika-worker-utils package provides utilities for managing and communicating with web workers in JavaScript. It simplifies the process of offloading tasks to background threads, allowing for more efficient and responsive applications.
WorkerPool
The WorkerPool feature allows you to create a pool of web workers to handle multiple tasks concurrently. This can be particularly useful for tasks that are computationally intensive and can be parallelized.
const { WorkerPool } = require('troika-worker-utils');
const pool = new WorkerPool({
maxWorkers: 4,
workerScriptURL: 'path/to/worker/script.js'
});
pool.postMessage({ task: 'heavyComputation', data: { /* some data */ } })
.then(result => {
console.log('Result from worker:', result);
});
createWorker
The createWorker feature allows you to create a web worker from a function. This is useful for defining worker behavior inline without needing a separate worker script file.
const { createWorker } = require('troika-worker-utils');
const worker = createWorker(function() {
self.onmessage = function(e) {
const result = e.data * 2; // Example computation
self.postMessage(result);
};
});
worker.postMessage(10);
worker.onmessage = function(e) {
console.log('Result from worker:', e.data); // Should log 20
};
The workerpool package provides a similar functionality to troika-worker-utils by allowing you to create a pool of web workers to handle parallel tasks. It offers a flexible API for managing worker threads and handling task distribution. Compared to troika-worker-utils, workerpool has a more extensive API and additional features like dynamic worker creation and task prioritization.
Comlink is a package that simplifies the use of web workers by providing a proxy-based API for communication. It allows you to call functions in the worker as if they were local, making the worker communication more intuitive. While troika-worker-utils focuses on worker management and task distribution, Comlink emphasizes ease of use and seamless function calls between the main thread and workers.
troika-worker-utils
This package provides utilities for making Web Workers easier to use.
Troika's "Worker Modules" system, exposed by the defineWorkerModule
export, allows you to define a function that will be executed with a web worker. This provides a simple interface for moving chunks of logic off the main thread, which can be critical in WebGL/WebXR scenes where frame rate cannot be interrupted by long-running code.
Similar utilities like Greenlet have existed for a while. However, defineWorkerModule
introduces the ability for worker modules to depend on one another. This means you can define modular chunks of code in separate functions, and then inject them into a worker where they can reference and call each other.
defineWorkerModule(options)
This function defines a Worker Module. It takes an options
object that can contain the following:
Required. This is the main function that initializes the module; it will be executed within the Worker the first time it is invoked. If any dependencies
are defined, the resolved values of those dependencies will be passed in as arguments.
Its return value becomes the module's "value". That can be:
A function, which can be called any number of times from the main thread by calling the function returned from defineWorkerModule()
.
Any other value, which will be used as the value passed to the init
of other worker modules using it as a dependency.
Note: As with any function-in-worker utility, the
init
function must not use any variables from the parent closure in which it is defined; its internal content must be completely standalone. Any external values you want to use must be passed in asdependencies
.
An optional array of dependencies required by the init function. Dependencies can be:
Primitives like strings, numbers, booleans
Functions; these will be stringified and rehydrated within the worker so they must not depend on anything from their parent closures
Other worker modules created by defineWorkerModule
; these will be resolved within the worker, and therefore modules that provide functions can be called without having to cross the worker/main thread boundary.
An optional function that will be run in the worker just before posting the response value from a module call back to the main thread. This function will be passed that response value, and if it returns an array then that will be used as the "transferables" parameter to postMessage
. Use this if there are values in the response that can/should be transfered rather than cloned.
An optional descriptive name for this module; this can be useful for debugging (it will be inserted as a comment into the Blob sent to the worker) but is not currently used for anything else.
By default all modules will run in the same dedicated worker, but if you want to use multiple workers you can pass a workerId
string to indicate a specific worker to spawn. Note that each worker is completely standalone and no data or state will be shared between them. If a worker module is used as a dependency by worker modules using different workerId
s, then that dependency will be re-registered in each worker.
The value returned by defineWorkerModule
is a function. If your options.init
returned a function, then this will be how you can invoke that within the worker. Call it, and it will give you a Promise for its return value.
import { defineWorkerModule } from 'troika-worker-utils'
// A simple module with a value:
const workerModuleA = defineWorkerModule({
init: function() {
return Math.PI
}
})
// A module that depends on the previous module:
const workerModuleB = defineWorkerModule({
dependencies: [
workerModuleA
],
init: function(moduleAValue) {
// moduleAValue here is "I'm the value of Module A!" from the first init function
// This return function can be invoked by calling workerModuleB in the main thread:
let callCount = 0
return function(arg) {
return `Called module B ${++callCount} times, `
+ `with arg "${arg}". Module A's value was ${moduleAValue}.`
}
}
})
workerModuleB('foo') // "Called module B 1 times, with arg "foo". Module A's value was 3.141592653589793."
workerModuleB('bar') // "Called module B 2 times, with arg "bar". Module A's value was 3.141592653589793."
FAQs
Utilities for executing code in Web Workers
The npm package troika-worker-utils receives a total of 168,621 weekly downloads. As such, troika-worker-utils popularity was classified as popular.
We found that troika-worker-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.
Security News
Sonar’s acquisition of Tidelift highlights a growing industry shift toward sustainable open source funding, addressing maintainer burnout and critical software dependencies.